package com.android.gufengmall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.R;
import com.android.gufengmall.bean.GoodsBean;

import org.w3c.dom.Text;

import java.util.List;

public class MallManagerAdapter extends RecyclerView.Adapter<MallManagerAdapter.MallManagerHolder> {

    private Context mContext;
    private List<GoodsBean> mGoodsBeanList;
    private onCheckSelectListener mListener;

    public MallManagerAdapter(Context context, List<GoodsBean> goodsBeanList) {
        mContext = context;
        mGoodsBeanList = goodsBeanList;
    }

    @NonNull
    @Override
    public MallManagerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MallManagerHolder(LayoutInflater.from(mContext).inflate(R.layout.item_mall_manager, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MallManagerHolder holder, int position) {
        GoodsBean goodsBean = mGoodsBeanList.get(position);
        holder.mTvName.setText(goodsBean.getGoods_name());
        holder.mTvPrice.setText(goodsBean.getGoods_price() + "");
        holder.mCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            mListener.OnCheckSelect(position, isChecked);
        });
    }

    @Override
    public int getItemCount() {
        return mGoodsBeanList.size();
    }

    public class MallManagerHolder extends RecyclerView.ViewHolder {

        private final CheckBox mCheckBox;
        private final TextView mTvName;
        private final TextView mTvPrice;

        public MallManagerHolder(@NonNull View itemView) {
            super(itemView);

            mTvName = itemView.findViewById(R.id.tv_name);
            mTvPrice = itemView.findViewById(R.id.tv_price);
            mCheckBox = itemView.findViewById(R.id.chx_select);
        }
    }

    public interface onCheckSelectListener {
        void OnCheckSelect(int position, boolean isCheck);
    }

    public void setOnCheckSelectListener(onCheckSelectListener onCheckSelectListener) {
        mListener = onCheckSelectListener;
    }
}
